package com.leyou.controller;


import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.pojo.PageResult;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.TbBrand;
import com.leyou.item.service.TbBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌表，一个品牌下有多个商品（spu），一对多关系 前端控制器
 * </p>
 *
 * @author mybaitsplus
 * @since 2019-10-18
 */
@RestController
@RequestMapping("/brand")//品牌
public class TbBrandController {
    @Autowired
    private TbBrandService brandService;
    /*
    * 品牌分页查询
    * */
    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows,
            @RequestParam(value = "key", required = false)String key,//关键词
            @RequestParam(value = "sortBy", required = false)String sortBy,//排序根据
            @RequestParam(value = "desc", defaultValue = "false")Boolean desc//排序规则
    ){
        return ResponseEntity
                .ok(brandService.searchPage(page,rows, key, sortBy, desc));
    }

    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> save(TbBrand brand, @RequestParam(name = "cids") List<Long> cids){
        brandService.saveBrandAndCategory(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }
    /*
    * 品牌信息修改，
    * 分类中间表，品牌表
    * */
    @PutMapping
    public ResponseEntity<Void> update(TbBrand brand,@RequestParam("cids") List<Long> cids){
        brandService.updateBrandAndCategory(brand,cids);
        return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /*
    * 分类ID找品牌
    * 商品增加步骤一
    * */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> findByCategoryId(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(this.brandService.findBrandByCategoryId(categoryId));
    }
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable(name = "id") Long id) {

        return ResponseEntity.ok( BeanHelper.copyProperties(brandService.getById(id),BrandDTO.class));

    }

    /**
     * 查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> findBrandsByIds(@RequestParam(name = "ids") List<Long> ids){
        Collection<TbBrand> tbBrandCollection = brandService.listByIds(ids);
        if(CollectionUtils.isEmpty(tbBrandCollection)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        List<BrandDTO> brandDTOList = tbBrandCollection.stream().map(tbBrand -> {
            return BeanHelper.copyProperties(tbBrand, BrandDTO.class);
        }).collect(Collectors.toList());
        return ResponseEntity.ok(brandDTOList);
    }




}

